﻿namespace MicroCloud.Utils
{
    /// <summary>
    /// 数值扩展操作类
    /// System.Decimal、System.Double、System.Int16、System.Int32、System.Int64
    /// </summary>
    public static class NumericalExtensions
    {
        #region "扩展方法"
        #region "从Decimal转换至Double"
        /// <summary>
        /// 从Decimal转换至Double
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static double ToDouble(this decimal value)
        {
            return Convert.ToDouble(value);
        }
        #endregion
        #region "从Int16转换至Double"
        /// <summary>
        /// 从Int16转换至Double
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static double ToDouble(this short value)
        {
            return Convert.ToDouble(value);
        }
        #endregion
        #region "从Int32转换至Double"
        /// <summary>
        /// 从Int32转换至Double
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static double ToDouble(this int value)
        {
            return Convert.ToDouble(value);
        }
        #endregion
        #region "从Int64转换至Double"
        /// <summary>
        /// 从Int64转换至Double
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static double ToDouble(this long value)
        {
            return Convert.ToDouble(value);
        }
        #endregion

        #region "从Double转换至Decimal"
        /// <summary>
        /// 从Double转换至Decimal
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this double value)
        {
            return Convert.ToDecimal(value);
        }
        #endregion
        #region "从Int16转换至Decimal"
        /// <summary>
        /// 从Int16转换至Decimal
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this short value)
        {
            return Convert.ToDecimal(value);
        }
        #endregion
        #region "从Int32转换至Decimal"
        /// <summary>
        /// 从Int32转换至Decimal
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this int value)
        {
            return Convert.ToDecimal(value);
        }
        #endregion
        #region "从Int64转换至Decimal"
        /// <summary>
        /// 从Int64转换至Decimal
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this long value)
        {
            return Convert.ToDecimal(value);
        }
        #endregion

        #region "Decimal中国式四舍五入"
        /// <summary>
        /// Decimal中国式四舍五入
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="decimals">保留小数</param>
        /// <returns></returns>
        public static decimal ChineseRounding(this decimal value, int decimals = 2)
        {
            return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
        }
        #endregion
        #region "Double中国式四舍五入"
        /// <summary>
        /// Double中国式四舍五入
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="decimals">保留小数</param>
        /// <returns></returns>
        public static double ChineseRounding(this double value, int decimals = 2)
        {
            return Math.Round(value, decimals, MidpointRounding.AwayFromZero);
        }
        #endregion
        #region "Single中国式四舍五入"
        /// <summary>
        /// Single中国式四舍五入
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="decimals">保留小数</param>
        /// <returns></returns>
        public static float ChineseRounding(this float value, int decimals = 2)
        {
            return Convert.ToSingle(Math.Round(value, decimals, MidpointRounding.AwayFromZero));
        }
        #endregion

        #region "Decimal四舍六入五取整"
        /// <summary>
        /// Decimal四舍六入五取整
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="decimals">保留小数</param>
        /// <returns></returns>
        public static decimal Round(this decimal value, int decimals = 2)
        {
            return Math.Round(value, decimals);
        }
        #endregion
        #region "Double四舍六入五取整"
        /// <summary>
        /// Double四舍六入五取整
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="decimals">保留小数</param>
        /// <returns></returns>
        public static double Round(this double value, int decimals = 2)
        {
            return Math.Round(value, decimals);
        }
        #endregion
        #region "Single四舍六入五取整"
        /// <summary>
        /// Single四舍六入五取整
        /// </summary>
        /// <param name="value">数值</param>
        /// <param name="decimals">保留小数</param>
        /// <returns></returns>
        public static float Round(this float value, int decimals = 2)
        {
            return Convert.ToSingle(Math.Round(value, decimals));
        }
        #endregion

        #region "Decimal向上取整"
        /// <summary>
        /// Decimal向上取整
        /// </summary>
        /// <param name="value">数值</param>
        /// <returns></returns>
        public static decimal Ceiling(this decimal value)
        {
            return Math.Ceiling(value);
        }
        #endregion
        #region "Double向上取整"
        /// <summary>
        /// Double向上取整
        /// </summary>
        /// <param name="value">数值</param>
        /// <returns></returns>
        public static double Ceiling(this double value)
        {
            return Math.Ceiling(value);
        }
        #endregion
        #region "Single向上取整"
        /// <summary>
        /// Single向上取整
        /// </summary>
        /// <param name="value">数值</param>
        /// <returns></returns>
        public static float Ceiling(this float value)
        {
            return Convert.ToSingle(Math.Ceiling(value));
        }
        #endregion

        #region "Decimal向下取整"
        /// <summary>
        /// Decimal向下取整
        /// </summary>
        /// <param name="value">数值</param>
        /// <returns></returns>
        public static decimal Floor(this decimal value)
        {
            return Math.Floor(value);
        }
        #endregion
        #region "Double向下取整"
        /// <summary>
        /// Double向下取整
        /// </summary>
        /// <param name="value">数值</param>
        /// <returns></returns>
        public static double Floor(this double value)
        {
            return Math.Floor(value);
        }
        #endregion
        #region "Single向下取整"
        /// <summary>
        /// Single向下取整
        /// </summary>
        /// <param name="value">数值</param>
        /// <returns></returns>
        public static float Floor(this float value)
        {
            return Convert.ToSingle(Math.Floor(value));
        }
        #endregion

        #endregion

    }
}
